package com.medical.standard.dao;

import com.medicalstandard.entity.Term;
import com.medicalstandard.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class TermDAO {

    public boolean addTerm(Term term) {
        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            conn = DBUtil.getConnection();
            String sql = "INSERT INTO term (standard_id, project_num, term_name, definition) VALUES (?, ?, ?, ?)";
            pstmt = conn.prepareStatement(sql);

            pstmt.setInt(1, term.getStandardId());
            pstmt.setString(2, term.getProjectNum());
            pstmt.setString(3, term.getTermName());
            pstmt.setString(4, term.getDefinition());

            int result = pstmt.executeUpdate();
            return result > 0;

        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            DBUtil.close(conn, pstmt);
        }
    }

    public boolean updateTerm(Term term) {
        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            conn = DBUtil.getConnection();
            String sql = "UPDATE term SET standard_id=?, project_num=?, term_name=?, definition=? WHERE term_id=?";
            pstmt = conn.prepareStatement(sql);

            pstmt.setInt(1, term.getStandardId());
            pstmt.setString(2, term.getProjectNum());
            pstmt.setString(3, term.getTermName());
            pstmt.setString(4, term.getDefinition());
            pstmt.setInt(5, term.getTermId());

            int result = pstmt.executeUpdate();
            return result > 0;

        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            DBUtil.close(conn, pstmt);
        }
    }

    public boolean deleteTerm(int termId) {
        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            conn = DBUtil.getConnection();
            String sql = "DELETE FROM term WHERE term_id=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, termId);

            int result = pstmt.executeUpdate();
            return result > 0;

        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            DBUtil.close(conn, pstmt);
        }
    }

    public List<Term> getTermsByStandardId(int standardId) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<Term> terms = new ArrayList<>();

        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT * FROM term WHERE standard_id=? ORDER BY project_num";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, standardId);
            rs = pstmt.executeQuery();

            while (rs.next()) {
                Term term = new Term();
                term.setTermId(rs.getInt("term_id"));
                term.setStandardId(rs.getInt("standard_id"));
                term.setProjectNum(rs.getString("project_num"));
                term.setTermName(rs.getString("term_name"));
                term.setDefinition(rs.getString("definition"));
                terms.add(term);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, pstmt, rs);
        }

        return terms;
    }

    public Term getTermById(int termId) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Term term = null;

        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT * FROM term WHERE term_id=?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, termId);
            rs = pstmt.executeQuery();

            if (rs.next()) {
                term = new Term();
                term.setTermId(rs.getInt("term_id"));
                term.setStandardId(rs.getInt("standard_id"));
                term.setProjectNum(rs.getString("project_num"));
                term.setTermName(rs.getString("term_name"));
                term.setDefinition(rs.getString("definition"));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, pstmt, rs);
        }

        return term;
    }
}